package com.nstar.orderexpress.view
{
	import com.nstar.base.util.Logger;
	import com.nstar.base.util.SystemUtils;
	import com.nstar.orderexpress.control.LoginEvent;
	import com.nstar.orderexpress.model.Model;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.controls.TextInput;
	import mx.core.Application;
	import mx.effects.Move;
	import mx.effects.Parallel;
	import mx.effects.Resize;
	import mx.events.CloseEvent;
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;

	public class LoginClass extends TitleWindow
	{
		public var btnOK:Button;
		public var btnCancel:Button;
		public var txtUserName:TextInput;
		public var txtPassword:TextInput;
		
		private var loggined:Boolean = false;
		
		private var model:Model = Model.getInstance();
		private var logger:Logger = Logger.getInstance();
		
		public function LoginClass()
		{
			//TODO: implement function
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
			this.addEventListener(KeyboardEvent.KEY_DOWN,keydownHandler);
			this.addEventListener(CloseEvent.CLOSE,closeHandler);
		}
		
		private function init(event:FlexEvent):void{
			this.x = this.parentApplication.width;
			this.y = -200;
			
			btnOK.addEventListener(MouseEvent.CLICK,okHandler);
			btnCancel.addEventListener(MouseEvent.CLICK,closeHandler);
			txtUserName.text = SystemUtils.getMemory("login","username");
			txtPassword.text = SystemUtils.getMemory("login","password");
			txtUserName.setFocus();
		}
		
		private function closeHandler(event:*):void{
			this.addEventListener(EffectEvent.EFFECT_END,effectEndHandler);
			playMoveEffect(true);
		}
		
		private function okHandler(event:MouseEvent):void{
			//验证用户信息
			//保存用户信息
			SystemUtils.setMemory("login","username",txtUserName.text);
			SystemUtils.setMemory("login","password",txtPassword.text);
			//设置登录状态
			loggined = true;
			this.parentApplication.dispatchEvent(new LoginEvent());
			closeHandler(null);
		}
		
		//支持登录后回车确认
		private function keydownHandler(event:KeyboardEvent):void{
			switch (event.keyCode){
				case Keyboard.ENTER:
					okHandler(null);
					break;
			}
		}
		
		//播放效果
		public function playMoveEffect(closed:Boolean, duration:Number=800):void{
			var par:Parallel = new Parallel(this);
			var resize:Resize = new Resize();
			resize.duration = duration;
			resize.end();
			

			var move:Move = new Move();
			move.duration = duration;
			move.end();
			
			if (closed){
				
				resize.widthTo = 0;
				resize.heightTo = 0;
				
				move.xTo = this.parentApplication.stage.width;
				move.yTo = 0;
			}else{
				this.removeEventListener(EffectEvent.EFFECT_END,effectEndHandler);
				resize.widthFrom = 0;
				resize.heightFrom = 0 ;
				resize.widthTo = 350;
				resize.heightTo = 300;
				
				move.xFrom = this.parentApplication.stage.width;
				move.yFrom = 0;
				move.xTo = (this.parentApplication.stage.width - this.width)/2;
				move.yTo = 50;
				
			}
			par.addChild(move);
			par.addChild(resize);
			par.play();
		}
		
		//关闭后效果播放完毕就关闭窗口
		private function effectEndHandler(event:EffectEvent):void{
			PopUpManager.removePopUp(this);
		}
		
	}
}